layui.config({
    base: "/static/plugs/"
}).use(['laydate', 'form', 'layer', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table,
        laydate = layui.laydate;

    /**
     * 消息组件实例
     */
    $.msg = new function () {
        var self = this;
        this.shade = [0.02, '#000'];
        this.dialogIndexs = [];
        //关闭消息框
        this.close = function (index) {
            return layer.close(index);
        };
        //弹出警告消息框
        this.alert = function (msg, callback) {
            var index = layer.alert(msg, {end: callback, scrollbar: false});
            return this.dialogIndexs.push(index), index;
        };
        //确认对话框
        this.confirm = function (msg, ok, no) {
            var index = layer.confirm(msg, {title: '操作确认', btn: ['确认', '取消']}, function () {
                typeof ok === 'function' && ok.call(this);
            }, function () {
                typeof no === 'function' && no.call(this);
                self.close(index);
            });
            return index;
        };
        // 显示成功类型的消息
        this.success = function (msg, callback) {
            if (callback == undefined) {
                callback = function () {
                }
            }
            var index = layer.msg(msg, {icon: 1, shade: this.shade, scrollbar: false, time: 2000, shadeClose: true}, callback);
            return this.dialogIndexs.push(index), index;
        };
        //显示失败类型的消息
        this.error = function (msg, callback) {
            if (callback == undefined) {
                callback = function () {
                }
            }
            var index = layer.msg(msg, {icon: 2, shade: this.shade, scrollbar: false, time: 3000, shadeClose: true}, callback);
            return this.dialogIndexs.push(index), index;
        };
        //状态消息提示
        this.tips = function (msg, time, callback) {
            var index = layer.msg(msg, {time: (time || 3) * 1000, shade: this.shade, end: callback, shadeClose: true});
            return this.dialogIndexs.push(index), index;
        };
        //显示正在加载中的提示
        this.loading = function (msg, callback) {
            var index = msg ? layer.msg(msg, {icon: 16, scrollbar: false, shade: this.shade, time: 0, end: callback}) : layer.load(2, {time: 0, scrollbar: false, shade: this.shade, end: callback});
            return this.dialogIndexs.push(index), index;
        };
    };

    /**
     * 常用工具
     */
    $.tool = new function () {
        //关闭当前弹出层
        this.close = function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
        //刷新当前弹出层
        this.reload = function () {
            var index = parent.layer.getFrameIndex(window.name);
            parent.location.reload();
        }
    }

    /**
     * 时间插件
     */
    $.date = new function () {
        var self = this;
        //年选择器
        this.year = function (elem) {
            laydate.render({elem: '#' + elem, type: 'year'});
        }
        //年月选择器
        this.month = function (elem) {
            laydate.render({elem: '#' + elem, type: 'month'});
        }
        //时间选择器
        this.time = function (elem) {
            laydate.render({elem: '#' + elem, type: 'time'});
        }
        //日期时间选择器
        this.datetime = function (elem) {
            laydate.render({elem: '#' + elem, type: 'datetime'});
        }
        //日期范围
        this.range = function (elem) {
            laydate.render({elem: '#' + elem, range: true});
        }
    }

    /**
     * 封装table插件
     */
    $.table = new function () {
        //渲染新的table
        this.create = function (elem, url, cols, page, limits, limit) {
            if (page == undefined) page = true;
            if (limits == undefined) limits = [10, 15, 20, 25];
            if (limit == undefined) limit = 20;
            id = elem + 'Table';
            table.render({
                elem: '#' + elem,
                url: url,
                cellMinWidth: 95,
                page: page,
                height: "full-125",
                limits: limits,
                limit: limit,
                id: id,
                cols: cols
            });
        }
    }

    /**
     * 封装请求
     */
    $.request = new function () {
        //post请求
        this.post = function (url, data, callback) {
            request('POST', url, data, callback);
        }
        //get请求
        this.get = function (url, data, callback) {
            request('GET', url, data, callback);
        }
    }

    /**
     * AJAX请求
     * @param type
     * @param url
     * @param data
     * @param callback
     */
    function request(type, url, data, callback) {
        $.msg.loading('正在加载，请稍等！');
        $.ajax({
            url: url,
            type: type,
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            dataType: "json",
            data: data,
            timeout: 60000,
            success: function (res) {
                log_ajax(type, url, res);
                $.msg.close();
                if (res.code == 0) {
                    callback(res);
                } else {
                    $.msg.error(res.msg);
                }
            },
            error: function (xhr, textstatus, thrown) {
                $.msg.error('Status:' + xhr.status + '，' + xhr.statusText + '，请稍后再试！');
            }
        });
    }

    /**
     * 记录AJAX请求
     * @param type
     * @param url
     * @param data
     */
    function log_ajax(type, url, data) {
        console.log('------------------------------------');
        console.log(type + '请求：' + url);
        console.log(data);
        console.log('------------------------------------');
    }
})